<template>
  <div class="app-container">
    <el-form :model="orderParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="用户id" prop="userId">-->
<!--        <el-input-->
<!--          v-model="orderParams.userId"-->
<!--          placeholder="请输入用户id"-->
<!--          clearable-->
<!--          @keyup.enter.native="orderQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="orderParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="orderQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="orderParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="orderQuery"
        />
      </el-form-item>
      <el-form-item label="支付金额" prop="price">
        <el-input
          v-model="orderParams.price"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter.native="orderQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="way">
        <el-input
          v-model="orderParams.way"
          placeholder="请输入支付方式"
          clearable
          @keyup.enter.native="orderQuery"
        />
      </el-form-item>
<!--      <el-form-item label="地址id" prop="addressId">-->
<!--        <el-input-->
<!--          v-model="orderParams.addressId"-->
<!--          placeholder="请输入地址id"-->
<!--          clearable-->
<!--          @keyup.enter.native="orderQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="所在地址" prop="address">
        <el-input
          v-model="orderParams.address"
          placeholder="请输入所在地址"
          clearable
          @keyup.enter.native="orderQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="orderQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetOrderQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="orderAdd"-->
<!--          v-hasPermi="['order:order:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="orderUpdate"-->
<!--          v-hasPermi="['order:order:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="submitOrderForm"-->
<!--          v-hasPermi="['order:order:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="orderExport"
          v-hasPermi="['order:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getOrderList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="orderSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
<!--      <el-table-column label="订单ID" align="center" prop="orderId" />-->
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="支付金额" align="center" prop="price" />
      <el-table-column label="支付方式" align="center" prop="way" />
<!--      <el-table-column label="类型" align="center" prop="type" />-->
<!--      <el-table-column label="地址id" align="center" prop="addressId" />-->
      <el-table-column label="所在地址" align="center" prop="address" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="orderDetail(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="orderUpdate(scope.row)"
            v-hasPermi="['order:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="orderDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="orderParams.pageNum"
      :limit.sync="orderParams.pageSize"
      @pagination="getOrderList"
    />

    <!-- 添加或修改用户订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
<!--        <el-form-item label="用户id" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入用户id" />-->
<!--        </el-form-item>-->
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="支付金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="支付方式" prop="way">
          <el-input v-model="form.way" placeholder="请输入支付方式" />
        </el-form-item>
<!--        <el-form-item label="地址id" prop="addressId">-->
<!--          <el-input v-model="form.addressId" placeholder="请输入地址id" />-->
<!--        </el-form-item>-->
        <el-form-item label="所在地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入所在地址" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="是否删除" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入是否删除" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrderForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/scp/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      orderParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        orderNo: null,
        price: null,
        way: null,
        addressId: null,
        address: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    /** 查询用户订单列表 */
    getOrderList() {
      this.loading = true;
      listOrder(this.orderParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.orderreset();
    },
    // 表单重置
    orderreset() {
      this.form = {
        orderId: null,
        userId: null,
        userName: null,
        orderNo: null,
        price: null,
        way: null,
        addressId: null,
        address: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    orderQuery() {
      this.orderParams.pageNum = 1;
      this.getOrderList();
    },
    /** 重置按钮操作 */
    resetOrderQuery() {
      this.resetForm("queryForm");
      this.orderQuery();
    },
    // 多选框选中数据
    orderSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //查看订单详情操作
    orderDetail(row){
      const orderId = row.orderId || this.ids
        this.$router.push({
          path: "/order/orderDetail",
          query:{
            orderId:orderId
          }
        })
    },
    /** 新增按钮操作 */
    orderAdd() {
      this.orderreset();
      this.open = true;
      this.title = "添加用户订单";
    },
    /** 修改按钮操作 */
    orderUpdate(row) {
      this.orderreset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data.order;
        this.open = true;
        this.title = "修改用户订单";
      });
    },
    /** 提交按钮 */
    submitOrderForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getOrderList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getOrderList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    orderDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除用户订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getOrderList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    orderExport() {
      this.download('order/order/export', {
        ...this.orderParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
